package com.yjps.system.controller;

import com.yjps.common.message.Message;
import com.yjps.common.page.Pager;
import com.yjps.common.util.CommonUtil;
import com.yjps.system.model.Order;
import com.yjps.system.model.User;
import com.yjps.system.service.OrderService;
import com.yjps.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * Created by jessy on 2015/8/26.
 */
@Controller
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    UserService userService;

    /**
     * 获得当前登录的门店经纪人的所有预约订单
     *
     * @param acceptResult
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/company/order/list", method = RequestMethod.GET)
    public String list(String acceptResult, HttpServletRequest request, HttpServletResponse response) throws Exception {
        /*当前门店经纪人登录 获取其id*/
        User user = (User) request.getSession().getAttribute("loginUser");
        Pager<Order> pager = orderService.getOrderByPager(new Pager<Order>(request, response), user.getId(), acceptResult);
        List<Order> orderList = pager.getList();
        for (Order o : orderList) {
            User u = userService.findUser(o.getUserId());
            o.setUsername(u.getUsername());
            o.setOrdermobile(u.getMobile());
        }
        Message message = null;
        if (pager.getList() != null && pager.getList().size() > 0)
            message = new Message(true, "获取订单列表成功！", pager);
        else
            message = new Message(false, "获取订单列表失败！");

        response.setHeader("Access-Control-Allow-Origin","*");

        return message.toJson();
    }

    /**
     * 用户下订单
     *
     * @param agentId 订单 门店经纪人id
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/company/order/edit", method = RequestMethod.GET)
    public String order(@RequestParam(value = "agentId", required = true) String agentId, HttpServletRequest request,HttpServletResponse response) {
        /*当前用户的id*/
        User user = (User) request.getSession().getAttribute("loginUser");
        Order order = new Order();
        order.setAgentId(Integer.parseInt(agentId));
        order.setUserId(user.getId());
        order.setOrderState(1);
        order.setOrderTime(CommonUtil.dataToString(new Date()));
        order.setAcceptTime("");
        order.setAcceptResult(0);
        order.setIsValid(1);
        int id = orderService.save(order);
        Message message = null;
        if (id > 0) {
            message = new Message(true, "预约成功！", order);
        } else {
            message = new Message(false, "预约失败！");

        }
        response.setHeader("Access-Control-Allow-Origin","*");

        return message.toJson();
    }

    /**
     * 受理订单
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/company/order/accept", method = RequestMethod.GET)
    public String accept(@RequestParam(value = "id", required = true) String id,HttpServletResponse response) {
        Message message = null;
        try {
            Order order = orderService.getOrder(Integer.parseInt(id));
            order.setAcceptResult(1);
            order.setAcceptTime(CommonUtil.dataToString(new Date()));
            orderService.updateOrder(order);
            message = new Message(true,"受理成功！");
        }catch (Exception e){
            message = new Message(false,"受理失败！");
        }finally {

        response.setHeader("Access-Control-Allow-Origin","*");
        }
        return message.toJson();
    }

}
